home *** CD-ROM | disk | FTP | other *** search
/ Sound Fx / Sound Fx.iso / Software / UNZIPED / MPW181-5 / _SETUP.1 / ibitstr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-19  |  10.0 KB  |  390 lines

  1. /* ibitstr.cpp
  2.  
  3.     Input bitstream class implementation
  4.  
  5.    Changes by Jeff Tsay:
  6.  
  7.     01/26/96 : Modified for Windows file handles.
  8.  
  9.    11/29/96 : Added syncword detection for compatibility with streams produced
  10.    by DALET. Changed at the request of Wilfried Solbach, thanks for the
  11.    donation in advance! However Layer I MPP files playback jerkily.
  12.  
  13.    03/09/97 : Added a routine to read layer III side info. Also not mentioned
  14.    previously are several routines that allow seeking in a stream.
  15.  
  16.    04/14/97 : Moved the side info routine to the layer III decoder object.
  17.    Revamped the seeking mechanism and also included better syncword detection.
  18.    Moved the original file reading mechanisms back for better
  19.    portability, and used John Fehr's implementations of the seeking routines
  20.     for non-Win32 file reading. */
  21.  
  22. /*
  23.  *  @(#) ibitstream.cc 1.8, last edit: 6/15/94 16:51:45
  24.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  25.  *  @(#) Berlin University of Technology
  26.  *
  27.  *  This program is free software; you can redistribute it and/or modify
  28.  *  it under the terms of the GNU General Public License as published by
  29.  *  the Free Software Foundation; either version 2 of the License, or
  30.  *  (at your option) any later version.
  31.  *
  32.  *  This program is distributed in the hope that it will be useful,
  33.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  34.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  35.  *  GNU General Public License for more details.
  36.  *
  37.  *  You should have received a copy of the GNU General Public License
  38.  *  along with this program; if not, write to the Free Software
  39.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  40.  */
  41.  
  42. /*
  43.  *  Changes from version 1.1 to 1.2:
  44.  *    - third argument in open syscall added
  45.  *    - minor bug in get_header() fixed
  46.  */
  47.  
  48. #ifdef  __WIN32__
  49. #define STRICT
  50. #include <windows.h>
  51. #else
  52. #include <fcntl.h>
  53. #include <io.h>
  54. #endif   // __WIN32__
  55.  
  56. #ifndef GUI
  57. #include <iostream.h>
  58. #endif // GUI
  59.  
  60. #include "all.h"
  61. #include "ibitstr.h"
  62. #include "header.h"
  63.  
  64. #ifdef  DAMN_INTEL_BYTE_ORDER
  65. #define swap_int32(int_32) (((int_32) << 24) | (((int_32) << 8) & 0x00ff0000) | \
  66.                                (((int_32) >> 8) & 0x0000ff00) | ((int_32) >> 24))
  67. #endif
  68.  
  69. Ibitstream::Ibitstream(const char *filename)
  70. {
  71.  
  72. #ifdef __WIN32__
  73.  
  74.   SECURITY_ATTRIBUTES security;
  75.   security.nLength              = sizeof(SECURITY_ATTRIBUTES);
  76.   security.lpSecurityDescriptor = NULL;
  77.   security.bInheritHandle       = FALSE;
  78.  
  79.   FH = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ,
  80.                   &security, OPEN_EXISTING, NULL, NULL);
  81.  
  82.   if (FH == INVALID_HANDLE_VALUE) {
  83.    char bad_file_msg[256];
  84.     lstrcpy(bad_file_msg, "Error opening file: ");
  85. #ifdef WIN32GUI
  86.     MessageBox(NULL, lstrcat(bad_file_msg, filename) , "Invalid handle", MB_OK);
  87. #else
  88.     cerr  << "Error opening file: " << filename << endl;
  89. #endif // WIN32GUI
  90.    return;
  91.   }
  92.  
  93. #else
  94.   // copy any arguments you need, open the file, and check for error
  95.  
  96.   if ((fd=open(filename,O_RDONLY)) == -1)
  97.   {
  98.           cerr << "Error opening file: " << filename << endl;
  99.       return;
  100.   }
  101.  
  102. #endif // __WIN32__
  103.  
  104.   wordpointer       = buffer;
  105.   bitindex          = 0;
  106.  
  107.   // Seeking variables
  108.   current_frame_number = -1;
  109.   last_frame_number    = -1;
  110. }
  111.  
  112. Ibitstream::~Ibitstream()
  113. {
  114.  
  115.   // close the file
  116. #ifdef __WIN32__
  117.   CloseHandle(FH);
  118. #else
  119.   if (fd) close(fd);
  120. #endif
  121.  
  122. }
  123.  
  124. BOOL Ibitstream::get_header (uint32 *headerstring, enum e_syncmode syncmode)
  125. {
  126.   BOOL result, sync;
  127.  
  128. #ifdef __WIN32__
  129.   DWORD  numread;
  130. #else
  131.   uint32 numread;
  132. #endif
  133.  
  134.   do {
  135.  
  136.     // Read 4 bytes from the file, placing the number of bytes actually
  137.    // read in numread
  138. #ifdef __WIN32__
  139.    result = (BOOL) ReadFile(FH, (char *) headerstring, 4, &numread, NULL);
  140. #else
  141.     if (fd == -1) return(FALSE); // check for error opening file
  142.     result = (BOOL) ((numread=read(fd, (char*) headerstring, 4)) == 4);
  143. #endif
  144.  
  145.    if (!(result && (numread == 4)))
  146.       return(FALSE);
  147.  
  148. #ifdef DAMN_INTEL_BYTE_ORDER
  149.     if (syncmode == INITIAL_SYNC) {
  150.        sync =  ((*headerstring & 0x0000F0FF) == 0x0000F0FF);
  151.    } else {
  152.        sync =  ((*headerstring & 0x000CF8FF) == syncword) &&
  153.              (((*headerstring & 0xC0000000) == 0xC0000000) == single_ch_mode);
  154.    }
  155. #else
  156.     if (syncmode == INITIAL_SYNC) {
  157.         sync =  ((*headerstring & 0xFFF00000) == 0xFFF00000);
  158.  
  159.     } else {
  160.        sync =  ((*headerstring & 0xFFF80C00) == syncword) &&
  161.              (((*headerstring & 0x000000C0) == 0x000000C0) == single_ch_mode);
  162.    }
  163. #endif // DAMN_INTEL_BYTE_ORDER
  164.  
  165.    if (!sync)
  166.      // rewind 3 bytes in the file so we can try to sync again, if
  167.    // successful set result to TRUE
  168. #ifdef __WIN32__
  169.       result = (BOOL) SetFilePointer(FH, -3, NULL, FILE_CURRENT);
  170. #else
  171.         result = (BOOL) (lseek(fd, -3, SEEK_CUR) != -1);
  172. #endif // __WIN32__
  173.  
  174.   } while (!sync && result);
  175.  
  176.   if (!result)
  177.      return(FALSE);
  178.  
  179. #ifdef DAMN_INTEL_BYTE_ORDER
  180.   uint32 header = *headerstring;
  181.   *headerstring = swap_int32(header);
  182. #endif // DAMN_INTEL_BYTE_ORDER
  183.  
  184.   if (last_frame_number < ++current_frame_number)
  185.       last_frame_number = current_frame_number;
  186.  
  187.   return(TRUE);
  188. }
  189.  
  190. BOOL Ibitstream::read_frame(uint32 bytesize)
  191. {
  192.  
  193.   DWORD numread;
  194.  
  195.   // read bytesize bytes from the file, placing the number of bytes
  196.   // actually read in numread and setting result to TRUE if
  197.   // successful
  198. #ifdef __WIN32__
  199.   BOOL result = ReadFile(FH, buffer, bytesize, &numread, NULL);
  200. #else
  201.   BOOL result = (BOOL) ((numread=read(fd,buffer,bytesize)) != -1);
  202. #endif // __WIN32__
  203.  
  204.  
  205. /*  if (bytesize > (bufferintsize << 2))
  206.   {
  207.      cerr << "Internal error: framelength > bufferlength?!\n";
  208.      exit (1);
  209.   } */
  210.  
  211.   wordpointer = buffer;
  212.   bitindex    = 0;
  213.   framesize   = bytesize;
  214.  
  215. #ifdef DAMN_INTEL_BYTE_ORDER
  216.   uint32 *wordp;
  217.   for (wordp = buffer + ((bytesize - 1) >> 2); wordp >= buffer; --wordp)
  218.   {
  219.  
  220. #ifdef INTEL386
  221.  
  222.   // inline 386 assembly doesn't work on Borland compiler
  223.  
  224.   __asm {
  225.       mov eax, wordp
  226.         mov ebx, [eax]
  227.       bswap ebx
  228.       mov [eax], ebx
  229.   }
  230.  
  231. #else
  232.  
  233.      uint32 word   = *wordp;
  234.     *wordp = swap_int32(word);
  235.  
  236. #endif // INTEL386
  237.  
  238.   } // for (wordp ...
  239.  
  240. #endif // DAMN_INTEL_BYTE_ORDER
  241.  
  242.   return((BOOL) (result && (numread == framesize)));
  243. }
  244.  
  245. uint32 Ibitstream::get_bits(uint32 number_of_bits)
  246. {
  247.   static uint32 bitmask[18] =
  248.   {
  249.      0,    // dummy
  250.      0x00000001, 0x00000003, 0x00000007, 0x0000000F,
  251.      0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
  252.      0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
  253.      0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
  254.     0x0001FFFF
  255.   };
  256.   uint32 returnvalue;
  257.   uint32 sum = bitindex + number_of_bits;
  258.  
  259.   if (sum <= 32)
  260.   {
  261.      // all bits contained in *wordpointer
  262.      returnvalue = (*wordpointer >> (32 - sum)) & bitmask[number_of_bits];
  263.      if ((bitindex += number_of_bits) == 32)
  264.      {
  265.         bitindex = 0;
  266.  
  267. /*        if ((char *)++wordpointer > (char *)buffer + framesize)
  268.         {
  269.     cerr << "Ibitstream::get_bits(): no more bits in buffer!\n";
  270.     exit (1);
  271.         } */
  272.         wordpointer++; // added by me!
  273.      }
  274.      return(returnvalue);
  275.   }
  276.  
  277. #ifdef DAMN_INTEL_BYTE_ORDER
  278.   *((int16 *)&returnvalue + 1) = *(int16 *)wordpointer;
  279.   wordpointer++; // Added by me!
  280.   *(int16 *)&returnvalue = *((int16 *)wordpointer + 1);
  281. #else
  282.   *(int16 *)&returnvalue = *((int16 *)wordpointer + 1);
  283.   wordpointer++; // Added by me!
  284.   *((int16 *)&returnvalue + 1) = *(int16 *)wordpointer;
  285. #endif // DAMN_INTEL_BYTE_ORDER
  286.  
  287.   returnvalue >>= 48 - sum;    // returnvalue >>= 16 - (number_of_bits - (32 - bitindex))
  288.   returnvalue &= bitmask[number_of_bits];
  289.   bitindex = sum - 32;
  290.  
  291.   return(returnvalue);
  292. }
  293.  
  294. void Ibitstream::set_syncword(uint32 syncword0)
  295. {
  296.  
  297. #ifdef DAMN_INTEL_BYTE_ORDER
  298.    syncword = swap_int32(syncword0 & 0xFFFFFF3F);
  299. #else
  300.     syncword = syncword0 & 0xFFFFFF3F;
  301. #endif // DAMN_INTEL_BYTE_ORDER
  302.  
  303.    single_ch_mode = ((syncword0 & 0x000000C0) == 0x000000C0);
  304. }
  305.  
  306. uint32 Ibitstream::file_size()
  307. // return the file size of the file
  308. {
  309.  
  310. #ifdef __WIN32__
  311.     return(GetFileSize(FH, NULL));
  312. #else
  313.     // hmm. I think this should work  -- John Fehr
  314.     off_t o_off;
  315.     off_t len;
  316.     o_off = lseek(fd, 0, SEEK_CUR);
  317.     len   = lseek(fd, 0, SEEK_END);
  318.     lseek(fd, o_off, SEEK_SET);
  319.     return(len);
  320. #endif // __WIN32__
  321.  
  322. }
  323.  
  324. #ifdef SEEK_STOP
  325.  
  326. BOOL Ibitstream::seek(int32 frame, int32 frame_size)
  327. // set the file pointer to frame * (frame_size + 4) bytes after the
  328. // beginning of the file and return TRUE if successful
  329. {
  330.     current_frame_number = frame - 1;
  331.  
  332. #ifdef __WIN32__
  333.     return(SetFilePointer(FH, frame * (frame_size + 4), NULL, FILE_BEGIN));
  334. #else
  335.  
  336.     return(lseek(fd, frame * (frame_size + 4), SEEK_SET));
  337. #endif // __WIN32__
  338.  
  339. }
  340.  
  341. BOOL Ibitstream::seek_pad(int32 frame, int32 base_frame_size,
  342.                           Header *header, uint32 *offset)
  343. {
  344.     // base_frame_size is the frame size _without_ padding.
  345.  
  346.     Crc16 *crc = NULL;
  347.  
  348.     int32 total_frame_size = base_frame_size + 4;
  349.     int32 diff;
  350.  
  351.     if (last_frame_number < frame) {
  352.         diff = (last_frame_number >= 0) ?  offset[last_frame_number] : 0;
  353.  
  354.        // set the file pointer to ((last_frame_number+1) * total_frame_size)
  355.       // bytes after the beginning of the file
  356. #ifdef __WIN32__
  357.         SetFilePointer(FH, (last_frame_number + 1) * total_frame_size +
  358.                             diff, NULL, FILE_BEGIN);
  359. #else
  360.         lseek(fd, (last_frame_number + 1) * total_frame_size + diff, SEEK_SET);
  361. #endif // __WIN32__
  362.  
  363.         current_frame_number = last_frame_number;
  364.  
  365.         do {
  366.             if (!header->read_header(this, &crc)) // will increment last_frame_number
  367.                 return(FALSE);
  368.         } while (last_frame_number < frame);
  369.  
  370.       return(TRUE);
  371.  
  372.     } else {
  373.         diff = (frame > 0) ? offset[frame - 1] : 0;
  374.  
  375.        // set the file pointer to (frame * total_frame_size  + diff) bytes
  376.       // after the beginning of the file
  377. #ifdef __WIN32__
  378.         SetFilePointer(FH, frame * total_frame_size + diff, NULL, FILE_BEGIN);
  379. #else
  380.         lseek(fd, frame * total_frame_size + diff, SEEK_SET);
  381. #endif // __WIN32__
  382.  
  383.         current_frame_number = frame - 1;
  384.  
  385.       return(header->read_header(this, &crc));
  386.     }
  387. }
  388. #endif // SEEK_STOP
  389.  
  390.